package com.ztom.test

import com.ztom.beans.factory.support.pojo.BeanDefinition
import com.ztom.beans.factory.support.instantiate.CglibSubclassingInstantiationStrategy
import com.ztom.beans.factory.DefaultListableBeanFactory
import com.ztom.test.service.TestService
import org.junit.jupiter.api.Test

/**
 * @author ZhangTao
 */
class Test02InstantiationStrategy {

    private val beanFactory: DefaultListableBeanFactory by lazy {
        val beanFactory = DefaultListableBeanFactory()
        beanFactory.apply {
            val beanDefinition = BeanDefinition(TestService::class)
            registerBeanDefinition("testService", beanDefinition)
        }
    }

    @Test
    fun testSimpleInstantiationStrategy() {
        // 默认策略: SimpleInstantiationStrategy
        val bean = beanFactory.getBean("testService")
        val testService = bean as TestService
        testService.doTest()
    }

    @Test
    fun testCglibSubclassingInstantiationStrategy() {
        // cglib 策略
        beanFactory.instantiationStrategy = CglibSubclassingInstantiationStrategy()
        val bean = beanFactory.getBean("testService")
        val testService = bean as TestService
        testService.doTest()
    }
}